﻿namespace EasyStep.TestActions.WebAii
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using EasyStep.Business.Interfaces;
    using ArtOfTest.WebAii.Core;
    using System.Drawing;
    using EasyStep.TestExecution.Interfaces;
    using EasyStep.TestExecution.Data;

    public class BrowserHelper
    {
        public static Settings GetBrowserSettings(string settingsBrowserKey, BrowserType defaultBrowserType)
        {
            SettingsConfigSectionHandler section = null;
            Settings settings = null;
            section = (SettingsConfigSectionHandler)System.Configuration.ConfigurationManager.GetSection("WebAii.Settings" + settingsBrowserKey);
            if (section == null)
            {
                section = (SettingsConfigSectionHandler)System.Configuration.ConfigurationManager.GetSection("WebAii.Settings");
                settings =section==null?new Settings(): new Settings(section);
                settings.Web.Browser = defaultBrowserType.ToBrowserExecutionType();
            }
            else settings = new Settings(section);
            settings.UnexpectedDialogAction = UnexpectedDialogAction.DoNotHandle;
            return settings;
        }


        public static Image MakeScreenshot(IExecuteDataContext DataContext, TestStepResult data)
        {
            try
            {
                var manager = (Manager)DataContext.BrowserInstance;
                data.Screenshot = manager.ActiveBrowser.Capture();
            }
            catch (Exception ex)
            {
                DataContext.Log.Fatal(string.Format("Can't create Screenshot:{0}", DataContext.TestHistoryManager.Convert(data)), ex);
            }
            return data.Screenshot;
        }
    }
}
